package com.ll.beautymall.ui.home.indexfragment.model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ll.beautymall.bean.Banner;
import com.ll.beautymall.bean.Category;
import com.ll.beautymall.bean.CategoryList;
import com.ll.beautymall.bean.ModelContent;
import com.ll.beautymall.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/20.
 * 具体加载首页轮播
 */
public class HeaderBiz {
    public void getBannerData(HashMap<String, String> parameters, final OnGetHeaderLisenter lisenter) {
        HttpUtils.getInstance().getBannerData(parameters, new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    try {
                        String json = response.body();
                        JSONObject obj = new JSONObject(json);
                        JSONObject data = obj.getJSONObject("data");
                        JSONArray items = data.getJSONArray("items");
                        Gson gson = new Gson();
                        List<Banner> banners = gson.fromJson(items.toString(), new TypeToken<List<Banner>>() {
                        }.getType());
                        if (banners != null) {
                            lisenter.onSucced(banners);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }

    public void getModelData(HashMap<String, String> parameters, final OnGetModelLisenter lisenter) {
        HttpUtils.getInstance().getModelData(parameters, new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    try {
                        String json = response.body();
                        JSONObject obj = new JSONObject(json);
                        JSONArray data = obj.getJSONArray("data");
                        Gson gson = new Gson();
                        List<ModelContent> result = gson.fromJson(data.toString(), new TypeToken<List<ModelContent>>() {
                        }.getType());
                        if (result != null) {
                            lisenter.onSucced(result);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }

    public void getNineData(final OnGetCategoryListLisenter lisenter) {
        HttpUtils.getInstance().getNineNavData(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    String jsonStr = response.body();
                    CategoryList categoryList = parseJson(jsonStr);
                    if (categoryList != null) {
                        lisenter.onSucced(categoryList);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }

    public void getCategoryList(HashMap<String, String> parameters, final OnGetCategoryListLisenter lisenter) {
        HttpUtils.getInstance().getCategoryList(parameters, new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    String json = response.body();
                    CategoryList categoryList = parseJson(json);
                    if (categoryList != null && lisenter != null) {
                        lisenter.onSucced(categoryList);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }

    private CategoryList parseJson(String json) {
        try {
            JSONObject obj = new JSONObject(json);
            JSONObject data = obj.getJSONObject("data");
            JSONArray category_list = data.getJSONArray("category_list");
            Gson gson = new Gson();
            List<Category> categories = gson.fromJson(category_list.toString(), new TypeToken<List<Category>>() {
            }.getType());
            String home_show_count = data.getString("home_show_count");
            String subtitle = data.getString("subtitle");
            String title = data.getString("title");
            CategoryList categoryList = new CategoryList(categories, home_show_count, subtitle, title);
            return categoryList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
